package com.sojson.util.encryption;

import java.io.IOException;
import java.util.Base64;
import java.util.Base64.Decoder;
import java.util.Base64.Encoder;

import com.sojson.constant.Constant;

/**
 * BASE64工具类
 * 
 * @author liu
 * @date 2020-09-04
 */
public class Base64Util {

    private final static Encoder encoder = Base64.getEncoder();
    private final static Decoder decoder = Base64.getDecoder();

    /**
     * 加密
     * 
     * @param content
     * @return
     * @throws IOException 
     */
    public static String encryption(String content) throws IOException {
        return encryption(content.getBytes(Constant.DEFAULT_ENCODING));
    }

    /**
     * 加密
     * 
     * @param content
     * @return
     */
    public static String encryption(byte[] content) {
        return encoder.encodeToString(content);
    }

    /**
     * 解密
     * 
     * @param content
     * @return
     */
    public static String decryption(String content) {
        return new String(decryptionToByte(content));
    }

    /**
     * 解密
     * 
     * @param content
     * @return
     */
    public static String decryption(byte[] content) {
        return new String(decryptionToByte(content));
    }

    /**
     * 解密为数组
     * 
     * @param content
     * @return
     */
    public static byte[] decryptionToByte(String content) {
        return decoder.decode(content);
    }

    /**
     * 解密为数组
     * 
     * @param content
     * @return
     */
    public static byte[] decryptionToByte(byte[] content) {
        return decoder.decode(content);
    }

}